/**
 * 
 */
package javavis.jip2d.functions;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javavis.base.JIPException;
import javavis.base.parameter.JIPParamDir;
import javavis.base.parameter.JIPParamFile;
import javavis.jip2d.base.FunctionGroup;
import javavis.jip2d.base.JIPFunction;
import javavis.jip2d.base.JIPImage;
import javavis.jip2d.base.bitmaps.JIPBmpFloat;
import javavis.jip2d.base.geometrics.JIPGeomPoly;
import javavis.jip2d.base.geometrics.Point2D;
import javavis.jip2d.base.geometrics.Polygon2D;
import javavis.jip2d.functions.tia2012.AdaBoost;
import javavis.jip2d.functions.tia2012.StrongLearner;

/**
 * @author asc
 *
 */
public class FFaceDetection extends JIPFunction {

	/**
	 * 
	 */
    private AdaBoost boosting;
	public FFaceDetection() {
	    name = "FFaceDetection";
        description = "Detecta eficientemente las caras de una imagen";
        groupFunc = FunctionGroup.Applic;
		// TODO Auto-generated constructor stub
	    JIPParamFile rutaAdaBoost = new JIPParamFile("AdaBoost", false, true);
		rutaAdaBoost.setDescription("Ruta de un fichero que contiene un AdaBoost");
		addParam(rutaAdaBoost);
	}

	/* (non-Javadoc)
	 * @see javavis.jip2d.base.JIPFunction#processImg(javavis.jip2d.base.JIPImage)
	 */
	@Override
	public JIPImage processImg(JIPImage img) throws JIPException {
		// TODO Auto-generated method stub
	    
	    lectura();
	    StrongLearner clasificadorFuerte = boosting.clasificadorFuerte;
	    JIPBmpFloat integral = (JIPBmpFloat) new FIntegralImage().processImg(new FColorToGray().processImg(img));
	    
	    int alto = integral.getHeight()-25;
	    int ancho = integral.getWidth()-25;
	    JIPGeomPoly poligonos = new JIPGeomPoly  ( img.getHeight(),img.getWidth());
	    for(int i =0; i < alto; i++)
	    {
	        for (int j = 0; j <ancho; j++)
            {
	            clasificadorFuerte.setAncho(j);
	            clasificadorFuerte.setAlto(i);
	            if(clasificadorFuerte.Clasifica(integral))
	            {
	                Polygon2D listapuntos = new Polygon2D();
	                listapuntos.addPoint(new Point2D(j, i));
	                listapuntos.addPoint(new Point2D(j+24, i));
	                listapuntos.addPoint(new Point2D(j+24, i+24));
	                listapuntos.addPoint(new Point2D(j, i+24));
	                poligonos.addPoly(listapuntos);
	            }
	            
            }
	        
	    }
	    
	    
		return poligonos;
	}

	 public void lectura(){

	        FileInputStream fis = null;
	        ObjectInputStream in = null;
	        try
	        {
	            fis = new FileInputStream(getParamValueString("AdaBoost"));
	            in = new ObjectInputStream(fis);
	            boosting = (AdaBoost)in.readObject();
	        } catch(IOException ioe)
	        {
	            System.err.println("Error de entrada/salida");
	        }catch (ClassNotFoundException cnfe)
	        {
	            System.err.println("Error de Clase no encontrada");
	        }
            catch (JIPException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
	    }

	    public void escritura(){

	        FileOutputStream fos=null;
	        ObjectOutputStream oos=null;
	        try {
	            fos = new FileOutputStream("backup");
	            oos = new ObjectOutputStream(fos);
	            oos.writeObject(boosting);
	        }
	        catch (Exception e) {
	            System.out.println("No se pudo guardar el fichero con el resultado del aprendizaje");
	            System.out.println(e);
	        }
	        finally {
	            try {
	                if (oos != null && fos != null) {
	                    oos.close();
	                    fos.close();
	                }
	            }
	            catch (Exception e) {System.out.println(e);}
	        }
	    }
	
}
